package com.wolfeyes.common.utils.framework.spring;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.ApplicationContext;

import com.wolfeyes.common.base.BaseConsumer;
//import cn.andsense.mq.utils.spring.SpringContextUtils;
import com.wolfeyes.common.utils.character.StringUtils;
//import cn.andsense.mq.utils.compiler.ClassLoaderUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * @description spring注册工具类
 * @author Administrator
 *
 */
@Slf4j
public class SpringRegisterUtils {

	private static final Map<String, BaseConsumer> baseConsumerMapCache = new ConcurrentHashMap<>();
	
	public static void initRegister(Class<?> entityClazz, BaseConsumer baseConsumer) {
		
		// 获取源代码中给出的底层类名的简称SimpleName
		//String simpleName = baseConsumer.getClass().getSimpleName();
		// bean名称（首字母小写转换toLowerCase）
		//String beanName = StringUtils.uncapitalize(simpleName);
		// 获取源代码中给出的底层类名的简称SimpleName
		String entitySimpleName = entityClazz.getSimpleName();
		// （首字母小写转换toLowerCase）
		String variableName = StringUtils.uncapitalize(entitySimpleName);
		
		ApplicationContext applicationContext = SpringContextUtils.getApplicationContext();
		// cn.andsense.mq.config.RabbitConfig$$Lambda$406/1143499107
		SpringDynamicAutowireBeanUtils.registerBeanForSingleton(applicationContext, baseConsumer, "$MQ$RabbitConfig$Lambda$" + variableName + "Consumer");
		
		
		String generate$MQPackagePath = "cn.andsense.mq.listener";
		//String generate$MQSimpleClassName = "$MQ" + entitySimpleName + "Listener";
		String generate$MQSimpleClassName = entitySimpleName + "MQListener";
		String generate$MQClassName = generate$MQPackagePath + "." + generate$MQSimpleClassName;
		//Class ListenerClazz = ClassLoaderUtils.newInstance(entityClazz);
		//Class targetInf = baseConsumer.getClass();
		try {
			Class ListenerClazz = Class.forName(generate$MQClassName);
			// 构造代理对象
			Constructor constructor = ListenerClazz.getConstructor();
			Object proxy = constructor.newInstance();
			SpringDynamicAutowireBeanUtils.registerBeanForSingleton(applicationContext, proxy);//默认beanName为$MQXXXListener
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		String entityQueueName = RabbitUtil.getDefaultQueueName(entityClazz);
		Queue entityQueue = new Queue(entityQueueName, true);
		SpringDynamicAutowireBeanUtils.registerBeanForSingleton(applicationContext, entityQueue, "$MQ$" + variableName + "Queue");
		String entityDirectExchangeName = RabbitUtil.getDefaultDirectExchangeName(entityClazz);
		DirectExchange entityDirectExchange = new DirectExchange(entityDirectExchangeName, true, false);
		SpringDynamicAutowireBeanUtils.registerBeanForSingleton(applicationContext, entityDirectExchange, "$MQ$" + variableName + "DirectExchange");
		String entityRoutingKeyName = RabbitUtil.getDefaultRoutingKeyName(entityClazz);
		Binding entityBinding = BindingBuilder.bind(entityQueue).to(entityDirectExchange).with(entityRoutingKeyName);
		SpringDynamicAutowireBeanUtils.registerBeanForSingleton(applicationContext, entityBinding, "$MQ$" + variableName + "Binding");
		//applicationContext.getBean("$MQDingTalkCallbackEncryptInfoListener");
		//ApplicationContextRegister.getBean("$MQDingTalkCallbackEncryptInfoListener");
		//Object beanInstance = SpringDynamicAutowireBeanUtils.getBeanForSecurity(applicationContext, "$MQMailListener", ListenerClazz);
	}
}
